﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAL;
using System.IO;
using ImageConverter;


namespace codeplexInteticsDev
{
    public partial class Form1 : Form, IMainView
    {
        Context db;
        ImageConv ic;
        public event Action CategoryChanged;
        public event Action RowSelected;

        public Form1()
        {
            InitializeComponent();
            db = new Context();
            cmbCategories.SelectedIndex = 0;
            ic = new ImageConv();
            dgvInfoWindow.DataSource = db.Products.ToList<Product>();

            cmbCategories.SelectedIndexChanged += (sender,args) => Invoke(CategoryChanged);
            dgvInfoWindow.SelectionChanged += (sender, args) => Invoke(RowSelected);
        }

        private void btnLoginWindow_Click(object sender, EventArgs e)
        {
            //frmLoginPass loginWindow = new frmLoginPass();
            //loginWindow.Show();
        }

        private void btnCreateDB_Click(object sender, EventArgs e)
        {
            //foreach (var item in db.Products)
            //{  
            //pboxProductImage.Image = ic.byteArrayToImage(db.Products.First<Product>(p=>p.image!=null).image);
            //}
        }

        private void cmbCategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            //select a category //------------------// Weapon //Hores //Alkohol
            if (cmbCategories.Text != "select a category" && cmbCategories.Text != "------------------")
            {
                
            }
        }


        private void dgvInfoWindow_CellContentClick(object sender, EventArgs e)
        {
            pboxProductImage.Image = ic.byteArrayToImage(db.Products.First<Product>(p => p.image != null).image);

            

            tbPrise.Text = dgvInfoWindow.Rows[dgvInfoWindow.CurrentRow.Index].Cells[2].FormattedValue.ToString();
            tbDescription.Text = dgvInfoWindow.Rows[dgvInfoWindow.CurrentRow.Index].Cells[3].FormattedValue.ToString();
            tbName.Text = dgvInfoWindow.Rows[dgvInfoWindow.CurrentRow.Index].Cells[1].FormattedValue.ToString();
            tbId.Text = dgvInfoWindow.Rows[dgvInfoWindow.CurrentRow.Index].Cells[0].FormattedValue.ToString();
        }

        public int IdFromDataGrid()
        {
            return (int)dgvInfoWindow.CurrentRow.Cells[0].Value;
        }

        public string GetCategory()
        {
            return cmbCategories.Text;
        }

        public void SetPictureSet(Image im)
        {
            pboxProductImage.Image = im;
        }

        public void SetId(string id)
        {
            tbId.Text = id;
        }

        public void SetName(string name)
        {
            tbName.Text = name;
        }

        public void SetPrice(string price)
        {
            tbPrise.Text = price;
        }

        public void SetDescription(string descr)
        {
            tbDescription.Text = descr;
        }

        private void Invoke(Action action)
        {
            if (action != null) action();
        }

        public new void Show()
        {
            Application.Run(this);
        }
    }
}
